package com.xjrsoft.module.customer.erp_model.eRP_InStock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customer.erp_model.eRP_InStock.dto.ErpStockinstockDto;
import com.xjrsoft.module.customer.erp_model.eRP_InStock.dto.SaveErpStockinstockFormDataDto;
import com.xjrsoft.module.customer.erp_model.eRP_InStock.vo.ErpStockinstockFormDataVo;
import com.xjrsoft.module.customer.erp_model.eRP_InStock.vo.ErpStockinstockListVo;
import com.xjrsoft.module.customer.erp_model.eRP_InStock.dto.ErpStockinstockListDto;
import com.xjrsoft.module.customer.erp_model.eRP_InStock.vo.ErpStockinstockentryVo;
import com.xjrsoft.module.customer.erp_model.eRP_InStock.entity.ErpStockinstockentry;
import com.xjrsoft.module.customer.erp_model.eRP_InStock.dto.ErpStockinstockentryDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.eRP_InStock.entity.ErpStockinstock;
import com.xjrsoft.module.customer.erp_model.eRP_InStock.vo.ErpStockinstockVo;
import com.xjrsoft.module.customer.erp_model.eRP_InStock.service.IErpStockinstockService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-10-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/erp_instock")
@Api(value = "入库管理", tags = "入库管理接口")
public class ErpStockinstockController {


	private final IErpStockinstockService erpStockinstockService;

	private final IXjrBaseCodeRuleService codeRuleService;

	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpStockinstock")
	public Response<ErpStockinstockFormDataVo> getFormData(@PathVariable String id) {
		ErpStockinstockFormDataVo formDataVo = new ErpStockinstockFormDataVo();
		// 主表数据
		ErpStockinstock erpStockinstock = erpStockinstockService.getById(id);
		formDataVo.setErpStockinstockVo(BeanUtil.copy(erpStockinstock, ErpStockinstockVo.class));
		// 从表数据
		formDataVo.setErpStockinstockentryVoList(erpStockinstockService.getInfo(erpStockinstock.getFId()));
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpStockinstock")
	public Response<PageOutput<ErpStockinstockListVo>> getPageList(ErpStockinstockListDto listDto) {
		IPage<ErpStockinstock> page = erpStockinstockService.getPageList(listDto);
		List<ErpStockinstockListVo> records = BeanUtil.copyList(page.getRecords(), ErpStockinstockListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpStockinstock")
	public Response save(@RequestBody SaveErpStockinstockFormDataDto formDto) {
		ErpStockinstockDto erpStockinstockDto = formDto.getErpStockinstockDto();
		ErpStockinstock erpStockinstock = BeanUtil.copy(erpStockinstockDto, ErpStockinstock.class);
		List<ErpStockinstockentryDto> erpStockinstockentryDtoList = formDto.getErpStockinstockentryDto();
		List<ErpStockinstockentry> erpStockinstockentryList = BeanUtil.copyList(erpStockinstockentryDtoList, ErpStockinstockentry.class);
		String dataId = erpStockinstockService.addErpStockinstock(erpStockinstock, erpStockinstockentryDtoList,erpStockinstockentryList);
		if (dataId != null) {
			codeRuleService.useEncode("InStockCode");
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpStockinstock")
	public Response update(@PathVariable String id, @RequestBody SaveErpStockinstockFormDataDto formDto) {
		ErpStockinstockDto erpStockinstockDto = formDto.getErpStockinstockDto();
		ErpStockinstock erpStockinstock = BeanUtil.copy(erpStockinstockDto, ErpStockinstock.class);

		List<ErpStockinstockentryDto> erpStockinstockentryDtoList = formDto.getErpStockinstockentryDto();
		List<ErpStockinstockentry> erpStockinstockentryList = BeanUtil.copyList(erpStockinstockentryDtoList, ErpStockinstockentry.class);
		return Response.status(erpStockinstockService.updateErpStockinstock(id, erpStockinstock, erpStockinstockentryList));
	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpStockinstockService.removeByIds(Func.toStrList(ids)));
	}

	
}
